/*-------------------------------------------------------------------------
   uart.c - serial i/o

   Copyright (C) 2010 One Laptop per Child

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!

-------------------------------------------------------------------------*/

#include <stdio.h>
#include "chip.h"
#include "uart.h"
#include "io.h"
#include "queue.h"
#include "timer.h"


static void polled_cout(unsigned char c)
{
	while (!TI);
	TI = 0;
	SBUF = c;
}

#if POLLED_UART

static void cout(unsigned char c)
{
	polled_cout(c);
}

void tx_drain( void )
{
}

char getchar()
{
	unsigned char c;
	while (!RI);
	c = SBUF;
	RI = 0;
	return c;
}

bool char_avail( void )
{
	return RI;
}

void uart_init()
{
	/* the UART part */
	SCON = 0x52;               // Serial Port Mode = 1

	SCON2 = 0x01;
	SCON3 = 0x1c;

	SBUF = '*';

	ES = 0;
	TI = 1;
}

#else

#define TX_QUEUE_DEPTH 64
#define RX_QUEUE_DEPTH 8
unsigned char xdata tx_queue[TX_QUEUE_DEPTH];
unsigned char xdata rx_queue[RX_QUEUE_DEPTH];
static unsigned char data tx_head;    // fixme try with these all as data
static unsigned char data tx_tail;
static unsigned char data rx_head;
static unsigned char data rx_tail;

static unsigned char data tx_off;

void uart_init(void)
{

	SCON = 0x52;
	SCON2 = 0x01;
	SCON3 = 0x1c;

	ES = 0;

	tx_off = 1;
	tx_head = tx_tail = 0;
	rx_head = rx_tail = 0;

	TI = 1;
	ES = 1;
}

void cout( unsigned char c)
{
	while (QUEUE_SIZE(tx_head,tx_tail) >= TX_QUEUE_DEPTH-2)
		;

	/* note -- under sdcc, just manipulating ES works okay.  but
	 * under Keil, we need to use EA, to disable all interrupts,
	 * otherwise we get crashes.
	 */
	EA = 0;

	tx_queue[tx_head++ & (TX_QUEUE_DEPTH-1)] = c;

	if (tx_off)
	{
		tx_off = 0;
		TI = 1;			/* enable it */
	}

	EA = 1;                         /* Enable Interrupts */
}

void tx_drain( void )
{
	while (QUEUE_SIZE(tx_head,tx_tail) != 0 )
		;
}

void uart_intsrv(void) interrupt 4
{
	if (RI)
	{
		RI = 0;
		if (QUEUE_SIZE(rx_head,rx_tail) < RX_QUEUE_DEPTH )
			rx_queue[rx_head++ & (RX_QUEUE_DEPTH-1)] = SBUF;
	}

	if (TI)
	{
		TI = 0;

		if (QUEUE_SIZE(tx_head,tx_tail) == 0 )
		{
			tx_off = 1;
		}
		else
		{
			SBUF = tx_queue[tx_tail++ & (TX_QUEUE_DEPTH-1)];
		}
	}

}

bool char_avail(void)
{
	return !!QUEUE_SIZE(rx_head,rx_tail);
}

char getchar(void)
{
	char c;

	while (QUEUE_SIZE(rx_head,rx_tail) == 0 )
		;

	ES = 0;
	c = rx_queue[rx_tail & (RX_QUEUE_DEPTH-1)];
	rx_tail++;
	ES = 1;

	return (c);
}

#endif  // POLLED_UART


void uart_calibrate(void)
{
	bit oES;
	int i;
	unsigned long s, os;

	puts("calibrate");
	tx_drain();

#define per100ms 3277  // 100ms @ 32.768Khz
	GPT2H = per100ms >> 8;
	GPT2L = per100ms & 0xff;
	GPTPF = 0x44;
	GPTCFG |= 0x04;

	oES = ES; ES = 0;

	for (i = 0; (GPTPF & 0x04) == 0; i++)
	{
		polled_cout('.');
	}

	GPTCFG &= ~0x04;

	ES = oES;  // save/restore -- works with POLLED_UART either 0 or 1

	os = (SCON2 << 8) | SCON3;

	// 1041 is observed "normal".  we'd expect 1152 10-bit chars
	// in 100ms, but back-to-back chars seem to have about 2.5
	// stop bits, so we can't simply calculate.
	s = i * os / 1041;

	SCON2 = s >> 8;
	SCON3 = s & 0xff;

	printf("\n%d/100ms: SCON2/3 %x --> %x\n", (uint)i, (uint)os, (uint)s);
}

#ifdef SDCC
void putchar(char c)
{
	cout(c);
	if (c=='\n') cout('\r');
}
#else // Keil
char putchar(char c)
{
	cout(c);
	if (c=='\n') cout('\r');
	return c;
}
#endif

